<template>
  <div
    v-elclickoutside="handleClose"
    ref="reference"
    :class="[disabled ? 'is_disabled' : 'is_valid', { is_focus: visible }]"
    wrap="wrap"
    class="wk-dep-select xh-form-border"
    @click="containerClick"
  >
    <div class="el-select__tags">
      <span
        v-for="(item, index) in showSelects"
        :key="index"
        class="user-item text-one-line"
        >{{ item.name }}
        <i class="delete-icon el-icon-close" @click.stop="deleteDep(index)" />
      </span>
      <i v-if="selects.length > max" class="el-icon-more" />
      <i :class="['el-icon-arrow-up', { 'is-reverse': visible }]" />
      <div v-if="selects.length == 0" class="user-placeholder text-one-line">
        {{ placeholder }}
      </div>

      <transition
        name="el-zoom-in-top"
        @before-enter="handleMenuEnter"
        @after-leave="doDestroy"
      >
        <wk-select-dropdown
          v-show="visible && !disabled"
          ref="popper"
          :append-to-body="popperAppendToBody"
        />
        <el-scrollbar ref="scrollbar" tag="div"> </el-scrollbar>
      </transition>
    </div>
  </div>
</template>

<script>
import WkSelectDropdown from "./src/SelectDropdown.vue";

import { objDeepCopy } from "@/utils";
const DefaultWkDepSelect = {
  value: "deptId",
  label: "name",
  // 主列表请求和参数 会和 props 内的合并（如果有值的情况下）
  request: null,
  params: null,
  dataType: "manage", // 部门的 value label 一致，用 dataType 区分
  isList: false, // 默认是树形接口，如果是列需设置为true
};
export default {
  name: "WkDepSelect",
  components: {
    WkSelectDropdown,
  },
  props: {
    radio: Boolean,
    // 展示限制
    max: {
      type: Number,
      default: 2,
    },
    props: {
      type: Object,
      default: () => {
        return {
          value: "id",
          label: "name",
        };
      },
    },
    placeholder: {
      type: String,
      default() {
        return "请选择";
      },
    },
    // eslint-disable-next-line vue/require-prop-types
    value: {
      required: true,
    },
    // 自定义请求和 参数
    request: Function,
    params: Object,
    options: Array,
    disabled: {
      type: Boolean,
      default: false,
    },
    popperAppendToBody: {
      type: Boolean,
      default: true,
    },
  },
  placeholder: {
    type: String,
    default() {
      return "请选择";
    },
  },
  data() {
    return {
      visible: false,
      dataValue: [], // 校准传入值
      loading: false,

      initView: false,
    };
  },
  computed: {
    optionsList() {
      if (this.config.dataType === "manage") {
        // 以缓存中的全部数据为id转对象的源
        return this.deptList;
      } else if (this.config.dataType === "hrm") {
        return this.hrmDeptList;
      }
    },
  },
  methods: {
    handleMenuEnter() {
      // 处理聚焦到具体位置
    },
    doDestroy() {
      this.$refs.popper && this.$refs.popper.doDestroy();
    },
    // 实际展示的数据
    showSelects() {
      if (this.selects && this.selects.length > this.max) {
        return this.selects.slice(0, this.max);
      }
      return this.selects;
    },
    // 选择的数据
    selects() {
      if (!this.optionsList.length) {
        return [];
      }

      return this.getSelectList();
    },
    /**
     * 获取展示的数组
     */
    getSelectList() {
      const list = [];
      this.recursionOptions(this.optionsList, this.dataValue, list);
      return list;
    },
    recursionOptions(options, selects, list) {
      options.forEach((item) => {
        if (selects.includes(item[this.config.value])) {
          list.push(item);
        }

        if (item.children) {
          this.recursionOptions(item.children, selects, list);
        }
      });
    },
    // 合并 props
    config() {
      const props = merge({ ...DefaultWkDepSelect }, this.props || {});
      if (this.request) {
        props.request = this.request;
      }

      if (this.params) {
        props.params = this.params;
      }
      return props;
    },
    handleClose() {
      this.visible = false;
    },
    containerClick() {
      if (!this.disabled) {
        this.initView = true;
        this.visible = !this.visible;
      }
    },
    /**
     * 删除
     */
    deleteDep(index) {
      if (!this.disabled) {
        // 直接 splice ,dataValue watch 的老旧值相同，复制之后新旧值不相同。暂时解决
        const dataValue = objDeepCopy(this.dataValue);
        dataValue.splice(index, 1);
        this.dataValue = dataValue;
        this.wkDepChange();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.wk-dep-select {
  height: 34px;
  position: relative;
  border-radius: 4px;
  font-size: 13px;
  background-color: white;
  border: 1px solid #e6e6e6;
  color: #333333;
  width: 180px;
  padding: 0 20px 0 5px;
  cursor: pointer;

  .user-item {
    padding: 5px 15px 5px 5px;
    background-color: #f3f7ff;
    border-radius: 4px;
    margin: 3px;
    max-width: 80px;
    position: relative;
  }
  .user-placeholder {
    color: #ddd;
    line-height: 34px;
    cursor: pointer;
    user-select: none;
  }
  .delete-icon {
    color: #999;
    cursor: pointer;
    position: absolute;
    top: 8px;
    right: 2px;

    &:hover {
      color: #2362fb;
    }
  }
  &:hover {
    border-color: #c0c4cc;
  }
}

.wk-dep-select.is_disabled {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  cursor: not-allowed;
  .user-item {
    background-color: #f0f4f8ea;
    color: #c0c4cc;
    cursor: not-allowed;
  }
  .delete-icon {
    color: #c0c4cc;
    cursor: not-allowed;
  }
  .user-placeholder {
    color: #c0c4cc;
    cursor: not-allowed;
  }
}

.wk-dep-select.is_valid:hover {
  border-color: #c0c4cc;
}

.wk-dep-select.is_focus {
  border-color: #2362fb !important;
}

.el-icon-more {
  position: absolute;
  top: 5px;
  right: 20px;
  padding: 6px 10px;
  font-size: 12px;
  background-color: #f3f7ff;
  color: #666;
  border-radius: 4px;
  &:hover {
    background-color: #2362fb;
    color: white;
  }
}

.el-icon-arrow-up {
  position: absolute;
  top: 10px;
  right: 5px;
  transition: transform 0.3s;
  color: #c0c4cc;
  font-size: 14px;
  transition: transform 0.3s;
  transform: rotate(180deg);
  cursor: pointer;
}
.el-icon-arrow-up.is-reverse {
  transform: rotate(0deg);
}
</style>
